package com.leetcode.p100;

import com.bean.TreeNode;
import com.utils.TreeUtils;

public class Test100 {

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }

        if (p == null || q == null || p.val != q.val) {
            return false;
        }

        return help(p.left, q.left) && help(p.right, q.right);
    }

    private boolean help(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }

        if (left != null && right == null) {
            return false;
        }
        if (left == null) {
            return false;
        }

        if (left.val != right.val) {
            return false;
        }

        return help(left.left, right.left) && help(left.right, right.right);
    }

    public static void main(String[] args) {
        TreeNode root = TreeUtils.buildTree();
        TreeNode root2 = TreeUtils.buildTree();
        TreeUtils.showTree(root);

        Test100 test = new Test100();
        boolean result = test.isSameTree(root, root2);
        System.out.println(result);
    }

}
